package com.newcapec.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.newcapec.dao.UserInfoDao;
import com.newcapec.entity.UserInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserInfoService {
    @Autowired
    private UserInfoDao userInfoDao;

//    public List<UserInfoEntity> selectAll(){
//        return userInfoDao.selectAll();
//    }
public List<UserInfoEntity> selectAll(UserInfoEntity userInfo){
    return userInfoDao.selectAll(userInfo);
}
//    public Map<String, Object> selectAll(int pageNum, int pageSize){
public Map<String, Object> selectAll(int pageNum, int pageSize,UserInfoEntity userInfo){
        //通过PageHelper对象设定分页参数
        PageHelper.startPage(pageNum, pageSize);
        //当前页面的数据
//        List<UserInfoEntity> list = userInfoDao.selectAll();
    List<UserInfoEntity> list = userInfoDao.selectAll(userInfo);
    //创建PageInfo对象，并将当前页面数据传入，计算分页相关数据
        PageInfo<UserInfoEntity> pageInfo = new PageInfo<>(list);
        long total = pageInfo.getTotal();
        Map<String, Object> map = new HashMap<>();
        //本页数据
        map.put("list", list);
        //总数据条目
        map.put("total", total);
        return map;

    }

    public UserInfoEntity login(String userName, String userPwd) {
        return userInfoDao.login(userName,userPwd);
    }

    public boolean addUser(UserInfoEntity userInfoEntity) {
        return userInfoDao.insertSelective(userInfoEntity);
    }

    public boolean update(UserInfoEntity userInfoEntity) {
        return userInfoDao.updateByPrimaryKeySelective(userInfoEntity);
    }

    public boolean deleteUser(int[] allUserId) {
//        return userInfoDao.batchDelete(allUserId);
        for(int userId:allUserId){
            userInfoDao.deleteByPrimaryKey(userId);
        }

        return true;
    }

    public boolean saveBatch(List<UserInfoEntity> list) {
        return userInfoDao.saveBath(list);
    }
}
